import { JSONMapping } from "../json-mapping";
import { Attachment } from "./attachment";
import { SyncStatus } from "../sync-status";
import {ObjectDtoInterface} from "../../services/sqliteDB/object.dto";

export enum WorkOrderType {
  UNKNOWN = 0,
  POWERCUT = 1,
  POWERRECOVERY = 2
}

export enum WorkOrderStatus {
  TO_BE_SUBMIT = 0, // 待提交申请
  TO_BE_APPROVED = 1, // 待审核
  REJECTED = 2,  // 审核拒绝
  APPROVED = 3, // 审核通过
  EXECUTED = 4, // 以执行停复电
}

export class WorkOrder extends JSONMapping implements ObjectDtoInterface {
    static PropertyKeyToJSONKeyMap() {
        return {
          id: "orderNo|id",
          userId: "customerNo|userId|powerCutCustomerNo",
          createdTime: "powerCutApplyTime|recoverApplyTime|createdTime",
          createdBy: "powerCutApplyAccount|recoverApplyAccount",
          dueTime: "powerCutTime|recoverTime|dueTime",
          reason: "powerCutReason|recoverApplyReason|reason",
          reasonId: "powerCutType",
          performTime: "performTime",
          type: "type|recordType",
          applyId: "applyId|powerCutApplyId|recoverApplyId",
          // sysUserId: "userAccount",
          powerCutApplicantName: "powerCutApplyName|recoverApproveName",
          powerCutApplicantId: "powerCutApplyAccount",
          powerCutApproverComment: "powerCutApproveComment",
          chargeDetailId: "powerCutChargeDetailId|chargeDetailId",
          powerCutPhotoIds: "powerCutPhotoIds|performPhotoId",
          powerCutThrumnailIds: "performThrumnailId|powerCutThrumnailIds"
        };
    }

    public id: string = null; // 停/复电申请ID
    public applyId: string = null;
    public userId: string = null; //
    public chargeDetailId: string = null; // 催费记录ID
    public type: WorkOrderType = null; // 1 停电工单，2 复电工单

    public syncStatus: SyncStatus = SyncStatus.None;
    public workOrderStatus: WorkOrderStatus = null; // 停电状态,

    public createdTime: number = null; // 提交记录的时间
    public updatedTime: number = null;
    public createdBy: string = null; //
    public updatedBy: string = null;

    //
    // 停电执行信息
    //
    public isPerform: number = null; // 是否执行停/复电0 否，1 是
    // public sysUserId: number = 0; // 提交记录的用户账号
    public performerOne: string = null; //
    public performerOneMobile: string = null; //
    public performerOneAccount: string = null; //
    public performerTwo: string = null; //
    public performerTwoAccount: string = null; //
    public performerTwoMobile: string = null; //
    public sealType: number = null; // 封印类型，1 封条 2 封印
    public sealNO: number = null; // 封条封印编号
    public sealDeviceId: string = null; // 加封设备ID
    public sealDeviceName: string = null; // 加封设备名
    public sealComponentId: string = null; // 加封设备位置ID
    public sealComponentName: string = null; // 加封设备位置名
    // public performPhotoId: string = ""; // 停电实施的图片Id
    public performTime: number = null; // 实施时间,
    // public performThrumnailId: string = null;  // 停电实施的缩略图片Id
    // public recordId: string = null; // 实施记录ID
    // public recordType: string = null; // 类型---确定是停电还是复电


    // 停电申请信息
    public dueTime: number = null; // 计划停/复电时间
    public powerCutImmediate: number = null; // 是否立即中止供电 0 否，1 是
    public reason: string = null; // 停复电原因
    public reasonId: string = null; // 停复电原因 Id
    public powerCutScope: string = null;   // 停电范围

    public powerCutPhotoIds: string = null; // 通知单图片ID –----几张？  会有多张情况
    public powerCutThrumnailIds: string = null; // 通知单图片缩略图ID
    // public powerCutApplyTime: number = 0;  // 停电申请时间
    public powerCutApplicantName: string = ""; //
    public powerCutApplicantId: string = "";
    public powerCutApproverComment: string = "";
    public powerCutApproverName: string = "";
    public powerCutApproveTime: number = null;
    // public recordId: string = null; // 实施记录ID
    // public recordType: string = null; // 类型---确定是停电还是复电
    // public recordDate: number = 0; // 是停电还是复电时间
    // public orderNo: string = null; // 订单编号

    // 表读数信息
    public meterReadingTotal: string = null; // 表底读数有功总
    public meterReadingTime: number = null; // 表底读数抄表日期

    public meterReadingEmpty: string = null; // 表底读数无功----------这些都是电表的读数
    public meterReadingVolumn: string = null; // 表底读数需量
    public meterReadingUp: string = null; // 表底读数峰
    public meterReadingDown: string = null; // 表底读数谷
    public meterReadingNormal: string = null; // 表底读数平

    // extra info, we don't use it now.
    // public applyId: string = null; // 停/复电申请id
    // public powerCutType: string = null; // 停电原因类型，1 欠费停电
    public powerCutApplyStatus: number = null;
    public recoverApplyStatus: number = null;
    public powerCutStatus: number = null;
    public recoverStatus: number = null;
    public powerCutApproveResult: number = null;

    public recordDate: number = null; // 是停电/复电实施时间
    public recordId: string = null; // 实施记录ID
    public payDate: number = null;

    // recoverApplyTime: string = null;
    recoverVerifyTime: string = null;

    public noticeAttachments: Array<Attachment> = [];

    constructor(json: { [key: string]: any }) {
        super(json);
        WorkOrder.mapFromJSON(json, this);
    }

    addNoticeAttachments(paths: Array<string>): Array<Attachment> {
        let attachments = paths.map((path, index) => {
          let id = new Date().getTime() + '' + (Math.ceil(Math.random() * 1000)) + '' + index;
          let name = path.split('/');
          return new Attachment({id: id , attachmentName: name[name.length - 1], path: path, syncStatus: SyncStatus.None});
        });

        // this.noticeAttachments = this.noticeAttachments.concat( attachments );
        return attachments;
      }

    updateAttachmentIdWithNewId(oldAttachmentId: string, newAttachmentId: string) {
      let attachment: Attachment = null;
      if (this.noticeAttachments.some((noticeAttachment, index) => {
        let result = noticeAttachment.id === oldAttachmentId;
        if (result) {
          attachment = noticeAttachment;
        }
        return result;
      })) {
        attachment.id = newAttachmentId;
      }
    }
}

